**********************************************************************
*Hospital year level regs 
*II. ANALYSIS
**********************************************************************
	*File options 
	if inlist($option ,1,3,5) {
	local is is
	}
	if inlist($option ,2,4,6) {
	local is ss
	}
	
	*CONTROL VARS
	{
	local hc2 bdtot teach mcare mcaid type_gac
	local area rural white college unempl poverty elderly state_exp_status 	
	local pc female age utilm90 
	local plan fullyinsured
	local hospcontrol `hc2' `area' hhi
	local ptntcontrol `pc' `plan' 
	local control never 
	local treat (incl_1`is')
	local fe i.year	
	local print keep(bought_1`is')	
	
	*Use CEM matching weights throughout 
	local WT "[aw=wt_cemm]"
	
	*Bootstraps
	local N 1000
	}
	
	use "$reg\FULLsample`is'", clear
	xtset num_prv year
	
	*1. Prices & patient experience
	*Tables 3, 6, A6, A9
	if inlist($option ,1,2) {	
	capture rm "$op\regs_FULL_`is'.xls"
	capture rm "$op\dvmean_FULL_`is'.xls"
	
	foreach var in cost17 z_avg {	
	local ptntcontrol `pc' `plan' 
	local drg tot_drgwt
	
	*Preferred DD
	xtreg `var' bought_1`is' `hospcontrol' `ptntcontrol' `drg' `fe' if (`treat' | `control') `WT', fe vce(cluster num_prv) 
	outreg2 using "$op\regs_FULL_`is'.xls", append `print' nocons ctitle("`is'_DW_`var'") 
	
	*Preferred event study 
	qui xtreg `var' pre3_1`is' pre2_1`is' pre0_1`is' post1_1`is' post2_1`is' post3_1`is'2 `hospcontrol' `ptntcontrol' `drg' `fe' if (`treat' | `control') `WT', fe vce(cluster num_prv) 
	test pre3_1`is' pre2_1`is' 
	gen pre = round(r(p),.01)
	outreg2 using "$op\regs_FULL_`is'.xls", append keep(pre* post*) nocons ctitle("`is'_nowt_otime3_`var'") 		
	*DV mean & pre-trend test 
	qui tabout pre1_1`is' if `treat' & pre1_1`is' using "$op\dvmean_FULL_`is'.xls"  `WT', sum cells(mean `var' N `var' mean pre) total("nowt") f(3 0 2) append	
	drop pre 
	}
	}
	
	*2. Inputs & volumes
	*Tables 5, A4, A8
	if inlist($option ,1,2) {	
	capture rm "$op\regs_FULL2_`is'.xls"
	
	local servreg n_servs n_techservs 
	local expreg  pbfexptot pbfpaytot2 pbfothexp pbfdeprinter 
	local ftereg  pbffte pbffteoth94 pbftotohfte5 pbftotcontractfte5 
	local volreg  admtot admtot_bed 
	if "`is'"=="is" {
	local volreg  `volreg' cardio_vol cardio_vol_bed 
	}
	
	foreach dv in `servreg' `expreg' `ftereg' `volreg' {
	local ptntcontrol 
	local drg 
	
	*Preferred DD
	xtreg `dv' bought_1`is' `hospcontrol' `ptntcontrol' `drg' `fe' if (`treat' | `control') `WT', fe vce(cluster num_prv) 
	outreg2 using "$op\regs_FULL2_`is'.xls", append `print' nocons ctitle("`is'_DW_`dv'") 
	
	*Event study 
	qui xtreg `dv' pre3_1`is' pre2_1`is' pre0_1`is' post1_1`is' post2_1`is' post3_1`is'2 `hospcontrol' `ptntcontrol' `drg' `fe' if (`treat' | `control') `WT', fe vce(cluster num_prv) 
	outreg2 using "$op\regs_FULL2_`is'.xls", append keep(pre* post*) nocons ctitle("`is'_DW_`dv'") 			
	
	*DV mean & pre-trend test 
	test pre3_1`is' pre2_1`is' 
	gen pre = round(r(p),.01)
	qui tabout pre1_1`is' if `treat' & pre1_1`is' `WT' using "$op\dvmean_FULL_`is'.xls", sum cells(mean `dv' N `dv' mean pre) f(3 0 2) total("`dv'") append	
	drop pre 
	}	
	}	
	
	*3. Robustness I
	*Tables 7, A10
	if inlist($option ,3,4) {
	capture rm "$op\regs_FULL3_`is'.xls"

	*Weights
	gen twt = wt_cemm * wts
	gen awt = admtot_first * wt_cemm
	
	foreach var in cost pbfexptot { 
	local ptntcontrol `pc' `plan' 
	local drg tot_drgwt
	if "`var'"=="pbfexptot" {
	local ptntcontrol 
	local drg 
	}
	
	*No controls 
	xtreg `var' bought_1`is' `fe'	 	if (`treat' | `control') `WT', fe vce(cluster num_prv) 
	outreg2 using "$op\regs_FULL3_`is'.xls", append `print' nocons ctitle("`is'_noc_`var'") 

	*No DRG weights
	xtreg `var' bought_1`is' `hospcontrol' `ptntcontrol' `fe'	    if (`treat' | `control') `WT', fe vce(cluster num_prv) 
	outreg2 using "$op\regs_FULL3_`is'.xls", append `print' nocons ctitle("`is'_noDW_`var'") 
	
	*DRG "fixed effects"  
	xtreg `var' bought_1`is' `hospcontrol' `ptntcontrol' d_* `fe'   if (`treat' | `control') `WT', fe vce(cluster num_prv) 
	outreg2 using "$op\regs_FULL3_`is'.xls", append `print' nocons ctitle("`is'_DRGFE_`var'") 
	
	*Narrower market definition (HHI using HSA) 
	ren hhi hhi_
	ren hhi_hsa hhi
	xtreg `var' bought_1`is' `hospcontrol' `ptntcontrol' `drg' `fe' if (`treat' | `control') `WT', fe vce(cluster num_prv) 
	outreg2 using "$op\regs_FULL3_`is'.xls", append `print' nocons ctitle("`is'_DWhhihsa_`var'") 
	ren hhi hhi_hsa 
	ren hhi_ hhi 
	
	*No matching
	xtreg `var' bought_1`is' `hospcontrol' `ptntcontrol' `drg' `fe' if (`treat' | `control'), fe vce(cluster num_prv) 
	outreg2 using "$op\regs_FULL3_`is'.xls", append `print' nocons ctitle("`is'_DWnomatch_`var'") 	
	
	*Patient weighted 
	if "`var'"=="cost17" {
	xtreg `var' bought_1`is' `hospcontrol' `ptntcontrol' `drg' `fe' if (`treat' | `control') [aw=twt], fe vce(cluster num_prv) 
	}
	else {
	xtreg `var' bought_1`is' `hospcontrol' `ptntcontrol' `drg' `fe' if (`treat' | `control') [aw=awt], fe vce(cluster num_prv) 
	}
	outreg2 using "$op\regs_FULL3_`is'.xls", append `print' nocons ctitle("`is'_volwt_`control'") 
	}
	capture drop awt bwt 
	}
	
	*4. Robustness II
	*Tables 7, A10
	if inlist($option ,3,4) {
	capture rm "$op\regs_FULL4_`is'.xls"
	
	foreach var in cost17 pbfexptot {
	capture gen ln_`var' = ln(`var')
	local ptntcontrol `pc' `plan' 
	local drg tot_drgwt
	if "`var'"=="pbfexptot" {
	local ptntcontrol 
	local drg 
	}
	
	*Log Y 
	xtreg ln_`var' bought_1`is' `hospcontrol' `ptntcontrol' `drg' `fe' if (`treat' | `control') `WT', fe vce(cluster num_prv) 
	outreg2 using "$op\regs_FULL4_`is'.xls", append `print' nocons ctitle("`is'_lnDW_`var'") 	
	
	*Non-neighbor control group 
	xtreg `var' bought_1`is' `hospcontrol' `ptntcontrol' `drg' `fe' if (`treat' | `control') & within!=1 `WT', fe vce(cluster num_prv) 
	outreg2 using "$op\regs_FULL4_`is'.xls", append `print' nocons ctitle("`is'_DW_`var'_exclwithin") 
	
	*Cluster by deal, HRR
	xtreg `var' bought_1`is' `hospcontrol' `ptntcontrol' `drg' `fe' if (`treat' | `control') `WT', fe vce(cluster deal_cluster) 
	outreg2 using "$op\regs_FULL4_`is'.xls", append `print' nocons ctitle("`is'_DW_clustdeal_`var'") 	
	
	xtreg `var' bought_1`is' `hospcontrol' `ptntcontrol' `drg' `fe' if (`treat' | `control') `WT', fe vce(cluster hrrcode) 
	outreg2 using "$op\regs_FULL4_`is'.xls", append `print' nocons ctitle("`is'_DW_clusthrr_`var'") 	

	*Matching on profits 
	ren wt_cemm_s main_wt_cemm_s 
	capture noisily merge 1:1 num ye using "$reg\fulldata_wts`is'_profits"
	capture drop if _m==2
	capture drop _m
	replace wt_cemm = 0 if missing(wt_cemm)
	
	xtreg `var' bought_1`is' `hospcontrol' `ptntcontrol' `drg' `fe' if (`treat' | `control') `WT', fe vce(cluster num_prv) 
	outreg2 using "$op\regs_FULL4_`is'.xls", append `print' nocons ctitle("`is'_DW_`var'_profitmatch") 	
	drop wt_cemm_s
	ren main_wt_cemm_s wt_cemm_s 

	*Balanced treatment panel
	xtreg `var' bought_1`is' `hospcontrol' `ptntcontrol' `drg' `fe' if (`treat' | `control') & bal_22_only==1 `WT', fe vce(cluster num_prv) 
	outreg2 using "$op\regs_FULL4_`is'.xls", append `print' nocons ctitle("`is'_DW_`var'_bal22only") 	
	
	*Exclude deal year 
	xtreg `var' bought_1`is' `hospcontrol' `ptntcontrol' `drg' `fe' if (`treat' | `control') & pre0_1`is'!=1 `WT', fe vce(cluster num_prv) 
	outreg2 using "$op\regs_FULL4_`is'.xls", append `print' nocons ctitle("`is'_DW_`var'_dropye0") 	
	
	*Treated soon control group (unmatched)
	if "`is'"=="is" {
	xtreg `var' bought_1`is' `hospcontrol' `ptntcontrol' `drg' `fe' if (`treat' | never_next_3yr), fe vce(cluster num_prv) 
	outreg2 using "$op\regs_FULL4_`is'.xls", append `print' nocons ctitle("`is'_DW_`var'_treatsoon") 	
	}
	}	
	}
		
	*5. Heterogeneity
	*Tables 4, A5, Figure A3a 
	if inlist($option ,3,4) {
	capture rm "$op\regs_FULL5_`is'.xls"
	capture rm "$op\regs_FULL6_`is'.xls"
	local bin_het pos_* hasin* acq_*p hi_*	
	unab bin_het: `bin_het'
	
	*Distance deciles 
	replace a_mindist = 9999 if missing(a_mindist) & incl_1`is'
	xtile dist_quant = a_mindist if incl_1`is' & wt_cemm > 0, n(10)
	
	foreach var in ln_cost ln_pbfexp { 
	if "`var'"=="ln_cost" {
	local ptntcontrol `pc' `plan' 
	local drg tot_drgwt
	}
	else {
	local pntcontrol 
	local drg 
	}
	
	foreach dist in dist_quant {
	forval i = 1/10 {
	local treatd (`treat' & `dist'<=`i')	
	
	qui xtreg `var' bought_1`is' `hospcontrol' `ptntcontrol' `drg' `fe' if (`treatd' | `control') `WT', fe vce(cluster num_prv) 
	outreg2 using "$op\regs_FULL5_`is'.xls", append `print' nocons ctitle("`is'_cDW_`var'_`i'") 			
	
	*N targets, average distances per decile
	distinct num if `treatd' & wt_cemm > 0 
	gen n1 = r(ndistinct)
	qui sum a_min if `treatd' & wt_cemm > 0
	gen max1 = r(max)
	
	qui tabout pre1_1`is' using "$op\dvmean_FULL_`is'.xls", sum cells(mean n1 mean max1) total("ntar_`i'") append 
	drop n1 max1 
	}
	}
	}
	
	*Other heterogeneity vars
	foreach var in cost17 pbfexptot {	
	local ptntcontrol `pc' `plan' 
	local drg tot_drgwt
	if "`var'"=="pbfexptot" {
	local ptntcontrol 
	local drg 
	}
	
	*Interacted regressions 
	foreach sh in `bin_het' {
	qui xtreg `var' bought_1`is'##`sh' `hospcontrol' `ptntcontrol' `drg' `fe' if (`treat' | `control') `WT', fe vce(cluster num_prv) 
	outreg2 using "$op\regs_FULL52_`is'.xls", append drop(`hospcontrol' `ptntcontrol' `drg' `fe') nocons ctitle("`is'_DW`sh'_`var'") 		
	
	*Subsample regressions 
	forval i = 0/1 { 	
	qui xtreg `var' bought_1`is' `hospcontrol' `ptntcontrol' `drg' `fe' if ((`treat' & `sh'==`i') | `control') `WT', fe vce(cluster num_prv) 
	outreg2 using "$op\regs_FULL52_`is'.xls", append `print' nocons ctitle("`is'_DW`sh'`i'_`var'") 			
	}
	}
	}	
	}
	
	*6. Robustness III Callaway Sant'Anna 
	*Table 7, A10
	if inlist($option ,3,4) {
	foreach var in cost17 pbfexptot p_read90_inp {
	
	if inlist("`var'","cost17","pbfexptot") {
	use "$reg\FULLsample`is'", clear
	xtset num_prv year
	}
	
	*For cardiac regressions, use a strict subset of the full regression sample
	else if inlist("`var'","p_read90_inp") {
	*Assumes the construction of a hospital-year level cardiac file, which contains 
	*readmission variables and has been merged to American Hospital Association (AHA) 
	*IDs and mergers. These processing steps are described in Appendix A1, A3. 
	use "$reg\hospreg_`file'_inp_readsantanna", clear
	merge 1:m num ye using "$reg\cardiosample`is'", keepusing(num)
	keep if _m==3
	drop _m
	gduplicates drop 
	isid num ye 
	merge 1:1 num ye using "$reg\FULLsample`is'", keepusing(wt_cemm)
	keep if _m==3
	drop _m
	xtset num_prv year
	}
	
	*Generate treatment cohorts 
	*aha_merge6_1.dta contains hospital-year level data from the AHA survey and 
	*identified mergers, described in Appendix A1, A3. 
	drop bought_`is'
	merge 1:1 num ye using "$aha\aha_merge6_1", keepusing(bought_`is')
	replace bought_`is' = . if incl_1`is'!=1
	gen boughtyr2 = year if bought_`is'==1
	bysort num: gegen boughtyr = min(boughtyr2)
	drop if _m==2
	drop _m boughtyr2
	
	gen g = 0 if never==1
	replace g = boughtyr if missing(g)
	drop if missing(g)
	keep if (`treat' | `control')
	
	*Integer weights
	foreach w in wt_cemm {
	ren `w' `w'2
	gen int `w' = round(`w'*1000,1)
	drop `w'2
	}
	
	local ptntcontrol `pc' `plan' 
	local drg tot_drgwt
	if "`var'"=="pbfexptot" {
	local ptntcontrol 
	local drg 
	}
	else if "`var'"=="p_read90_inp" {
	local ptntcontrol female age elix_score inp_util_cnt plan_prod1 plan_prod2 plan_prod3 plan_prod4 plan_rel1 plan_rel2 plan_rel3 indiv_aca indiv_offexchange_flg fullyinsured_flg 
	local drg tot_drgwt
	}
	
	*Callaway Sant'Anna 
	qui csdid `var' bought_1`is' `hospcontrol' `ptntcontrol' `drg' [iw=wt_cemm], i(num) t(ye) g(g) method(reg)
	estat all
	
	*Callaway Sant'Anna, notyet 
	qui csdid `var' bought_1`is' `hospcontrol' `ptntcontrol' `drg' [iw=wt_cemm], i(num) t(ye) g(g) method(reg) notyet
	estat all
	pause 
	}
	}
			
	*7. Standardized differences
	*Table A2 
	if inlist($option ,3,4) {
	use "$reg\FULLsample`is'", clear
	xtset num_prv year
	
	*Prep vars 
	merge 1:1 num year using "$reg\\`is'matchinput", keepusing(*mgn*)
	drop if _m==2
	drop _m 
	gen mcare_mcaid = mcare + mcaid 
	
	*Calc means + SDs 
	local inp cost17 pbfexptot pbfpaytot pbffte bdtot teach rural mcare_mcaid np mgn_ptnt 
	local cond "pre1_1`is' | (never & inrange(year,2012,2016))" 
	foreach var in `inp' {
	gegen m1_`var'  = mean(`var') if incl_1`is' & `cond'
	gegen sd1_`var' = sd(`var')   if incl_1`is' & `cond'
	gegen m2_`var'  = mean(`var') if never      & `cond'
	gegen sd2_`var' = sd(`var')   if never      & `cond'
	
	gegen m3_`var'  = mean(`var') if incl_1`is' & `cond' [aw=wt_cemm]
	gegen sd3_`var' = sd(`var')   if incl_1`is' & `cond' [aw=wt_cemm]
	gegen m4_`var'  = mean(`var') if never      & `cond' [aw=wt_cemm]
	gegen sd4_`var' = sd(`var')   if never      & `cond' [aw=wt_cemm]
	}
	
	foreach var in `inp' {
	forval i = 1/4 {
	ren m`i'_`var' t
	gegen m`i'_`var' = max(t)
	drop t 
	ren sd`i'_`var' t
	gegen sd`i'_`var' = max(t)
	drop t 
	}
	}
	keep m?_* sd?_* 
	gduplicates drop 
	
	*Calc standardized differences
	foreach var in `inp' {
	gen stddif_`var'  = (m1_`var' - m2_`var')
	gen stddifw_`var' = (m3_`var' - m4_`var')
	
	if inlist("`var'","teach","rural","np") {
	gen     d  = (sd1_`var'*(1-sd1_`var') + sd2_`var'*(1-sd2_`var'))/2
	gen     dw = (sd3_`var'*(1-sd3_`var') + sd4_`var'*(1-sd4_`var'))/2
	}
	else {
	gen     d  = (sd1_`var'^2 + sd2_`var'^2)/2
	gen     dw = (sd3_`var'^2 + sd4_`var'^2)/2
	}
	
	replace d  = d^0.5
	replace dw = dw^0.5
	replace stddif_`var'  = stddif_`var' / d
	replace stddifw_`var' = stddifw_`var' / dw
	drop d dw 
	}
	sum stddif* 
	pause 
	}
	
	*8. Test IS vs SS coefficients 
	*Figure 4 
	if $option ==5 | $option ==6 {
	keep num* ye cost17 pbfexptot bought_1`is' `treat' `control' `hospcontrol' `ptntcontrol' tot_drgwt wt_cemm 	
	
	*Bootstrap samples 
	*Sample providers N times, stratified by control status
	drop if wt_cemm==0 
	set seed 1234
	forval i = 1/`N' {
	gen bwt_`i' = 0 
	bsample, cluster(num_) weight(bwt_`i') strata(never)
	}
	qui compress 
	
	*Gen matching weights for each sample
	merge 1:1 num ye using "$reg\fulldata_wts`is'", keepusing(cem_strata)
	drop if _m==2
	drop _m 
	
	forval i = 1/`N' {
	*Drop treated if missing all control hospitals in strata
	qui gen tn = never & bwt_`i' > 0 
	qui bysort cem_strata: gegen ctn = sum(tn)	
	qui replace bwt_`i' = 0 if ctn==0 
	ren bwt_`i' temp 
	qui bysort num: gegen bwt_`i' = min(temp)	
	count if temp!=bwt_`i'
	drop ct? t? temp
	
	*Drop control hospitals if missing all treateds in all strata 
	qui gen ti = incl_1`is' & bwt_`i' > 0 
	qui gen temp = cem_strata 
	qui bysort cem_strata: gegen cti = sum(ti)	
	qui replace temp = . if cti==0
	qui bysort num: gegen ct = count(temp)
	replace bwt_`i' = 0 if never & ct==0
	qui drop ti temp ct*
	}
	
	forval i = 1/`N' {
	qui replace bwt_`i' = bwt_`i' * wt_cemm
	qui replace bwt_`i' = round(bwt_`i' * 100,1)
	}	

	*Bootstrapped regressions
	foreach dv in cost17 pbfexptot {
	preserve 
	if "`dv'"=="cost17" {
	local ptntcontrol `pc' `plan' 
	local drg tot_drgwt
	}
	else {
	local ptntcontrol 
	local drg 	
	}
		
	xtset num ye 
	sort num ye 
	gen B  = . 
	forval i = 1/`N' {
	di "`i'"
	local wt  [fw=bwt_`i']
	qui xtreg `dv' bought_1`is' `hospcontrol' `ptntcontrol' `drg' `fe' if (`treat' | `control') `wt', fe vce(cluster num_prv) 
	qui replace B = _b[bought_1`is'] in `i'
	}	
	
	*Save data 
	keep B* 
	drop if missing(B)
	if "`is'"=="is" {
	gen is = 1
	save "$reg\\`dv'_boot", replace 
	}
	else {
	gen is = 0 
	append using "$reg\\`dv'_boot"
	compress 
	save "$reg\\`dv'_boot", replace 
	replace B  = B / 1000
	
	*Test coefficients & plot distributions
	bysort is: sum B*
	ttest B, by(is)
	pause 
	twoway (hist B if is, percent fcolor(blue*1.5%30) lcolor(blue%30) xtitle("Effects on expenses per bed ($'000)", size(large)) ytitle("Percent", size(large)) legend(label(1 "Acquired independent") label(2 "Acquired system owned") ) graphregion(color(white)) ysize(2.78) xsize(5.5) xlabel(,labsize(large)) ylabel(,labsize(large))) (hist B if !is, percent fcolor(lime*1.2%30) lcolor(lime*1.2) lpattern(dash))
	graph export "$op\\`dv'_coef_test.png", as(png) replace
	}
	restore 
	}
	exit
	}
	
********************************************************************************
	*9. Summary stats 
	*Table 1
	if $option ==7 {
	
	*Cardiac Readmissions
	foreach s in is ss {
	use "$reg\cardiosample`s'", clear
	bysort num ye: gegen mean_read90 = mean(p_read90_inp)
	bysort num ye: gegen ct          = count(p_read90_inp)
	keep num ye mean_read90 ct
	gduplicates drop 
	tempfile cardio`s'
	save `cardio`s'', replace 
	}
	
	*Prices, controls 
	use "$reg\FULLsampleis", clear
	gen is = 1 
	append using "$reg\FULLsampless"
	replace is = 0 if missing(is)
	gen prev10 = never & !is
	gduplicates report num ye 
	
	merge 1:1 num ye using `cardiois'
	drop if _m==2
	drop _m 
	foreach var in mean_read90 ct {
	ren `var' `var'_
	}
	merge 1:1 num ye using `cardioss'
	drop if _m==2
	drop _m 
	foreach var in mean_read90 ct {
	replace `var' = `var'_ if missing(`var')
	drop `var'_
	}
	
	*Patient margin 
	merge 1:1 num ye using "$reg\ismatchinput", keepusing(mgn_ptnt)
	drop if _m==2
	drop _m 
	foreach var in mgn_ptnt {
	ren `var' `var'_
	}
	merge 1:1 num ye using "$reg\ssmatchinput", keepusing(mgn_ptnt)
	drop if _m==2
	drop _m 
	foreach var in mgn_ptnt {
	replace `var' = `var'_ if missing(`var')
	drop `var'_
	}
	
	*Summarize in first year 
	bysort num: gegen minye = min(year)
	gen ye1 = year==minye
	tab minye 

	gen twt = wt_cemm*ct 
	gen mcare_mcaid = mcare + mcaid 
	local hcontrol rural poverty bdtot mcare_mcaid np
	local inp mgn_ptnt cost17 pbfexptot pbfpaytot2 pbffte 
	local read mean_read90 
		
	foreach var in incl_1is incl_1ss never prev10 {
	gdistinct num if `var'==1 & wt_c > 0 
	replace `var' = 0 if missing(`var')
	}
	
	*(1) All, (2) never acquired, (3) previously owned, (4) acquired IS, (5) acquired SS  
	distinct num if wt_c > 0 
	gen n1 = r(ndistinct)
	distinct num if never & wt_c > 0 
	gen n2 = r(ndistinct)
	distinct num if prev10 & wt_c > 0 
	gen n3 = r(ndistinct)	
	distinct num if incl_1is & wt_c > 0 
	gen n4 = r(ndistinct)
	distinct num if incl_1ss & wt_c > 0 
	gen n5 = r(ndistinct)

	sum `hcontrol' `inp' n1 if ye1	          `WT'
	sum `hcontrol' `inp' n2 if never    & ye1 `WT'	
	sum `hcontrol' `inp' n3 if prev10   & ye1 `WT'
	sum `hcontrol' `inp' n4 if incl_1is & ye1 `WT'
	sum `hcontrol' `inp' n5 if incl_1ss & ye1 `WT'
		
	sum `read' if ye1	         [aw=twt]
	sum `read' if never    & ye1 [aw=twt]	
	sum `read' if prev10   & ye1 [aw=twt]
	sum `read' if incl_1is & ye1 [aw=twt]
	sum `read' if incl_1ss & ye1 [aw=twt]
	pause 
	}
		
********************************************************************************
	*10. Top DRGs table
	*Table A1
	if $option ==7 {
	*Assumes the construction of summary files containing all episodes (alllist)
	*and DRGs (FULL1_all_ptnt).
	use "$reg\alllist", clear
	merge 1:m num ye using "$reg\FULL1_all_ptnt", keepusing(drg)
	keep if _m==3
	drop _m
	
	bysort drg: gegen ct = count(ye)
	keep drg ct 
	gduplicates drop 
	gduplicates report drg 
	keep if ct > 950
	
	*drgwts is a DRG-level file containing DRG weights 
	merge 1:m drg using "$sdb\tarball\DRG\drgwts"
	keep drg ct drgdesc
	drop if missing(ct)
	gduplicates drop 
	gduplicates report drg 
	
	gsort drg -drgdesc
	bysort drg: gen n = _n
	keep if n==1
	drop n 
	gduplicates drop 
	gduplicates report drg
 	
	gsort -ct 
	list in 1/50 
	pause 
	}
	
********************************************************************************
	*11. MDC-specific regressions 
	*Tables 3, A6
	if inlist($option ,3,4) {
	capture rm "$op\regs_FULLmdc_`is'.xls"
	capture rm "$op\dvmean_FULLmdc_`is'.xls"
	
	foreach mdc in all 1 4 5 6 8 14 18 {
	*Assumes the construction of hospital-year level episode files for the top 7
	*MDCs from Elevance, which have been merged to American Hospital 
	*Association (AHA) IDs and mergers. These processing steps are described in 
	*Appendix A1, A3. 
	use "$reg\hospreg_FULL0_`mdc'1", clear
	xtset num_prv year
	replace cost17  = cost17 / 0.9762
	
	local lab1  "CNS"
	local lab4  "RESP"
	local lab5  "CARDIO"
	local lab6  "DIGEST"
	local lab8  "MUSC"
	local lab14 "DELIVERY"
	local lab18 "INFECT"
	
	*Use strict subsamples of full sample 
	foreach var in never incl_1`is' bought_1`is' pre* post* wt_cemm hhi {
	capture drop `var'
	}
	merge 1:1 num ye using "$reg\FULLsample`is'", keepusing(never incl_1`is' bought_1`is' pre* post* wt_cemm hhi)
	keep if _m==3
	drop _m	
	
	foreach var in cost17 {	
	local ptntcontrol `pc' `plan' 
	local drg tot_drgwt

	*DD
	qui xtreg `var' bought_1`is' `hospcontrol' `ptntcontrol' `drg' `fe' if (`treat' | `control') `WT', fe vce(cluster num_prv) 
	outreg2 using "$op\regs_FULL_`is'.xls", append `print' nocons ctitle("`is'_nowtDW_`var'_`lab`mdc''") 
	
	*DV mean 
	qui tabout pre1_1`is' if `treat' using "$op\dvmean_FULL_`is'.xls" `WT', sum cells(mean `var' N `var') total("nowt_`lab`mdc''") append	
	}
	}
	}
	
	*12. Robustness IV Patient level regressions 
	*Tables 7, A3, A7, A10
	if inlist($option ,3,4) {
	capture rm "$op\regs_FULLp_`is'.xls"
	capture rm "$op\dvmean_FULLp_`is'.xls"
	
	*Assumes the construction of an episode level file, which contains spending 
	*summary data and has been merged to American Hospital Association (AHA) 
	*IDs and mergers. These processing steps are described in Appendix A1, A3. 
	use "$reg\FULL0_all_ptnt", clear
	local fe i.year i.num_prv
	
	*Reweight Elevance sample to reflect NY DRG distribution 
	drop d_*
	ren drgnum drg 
	sort drg 
	*ny_drg_dist is a DRG-level file with counts of inpatient visits in the NY 
	*discharge data.
	merge m:1 drg using "$sdb\tarball\disch\ny_drg_dist", sorted 
	drop if _m==2
	drop _m
	ren ct ny_ct
	drop if missing(ny_ct)
	
	*Apply matching weights
	drop bought* incl* never 
	merge m:1 num ye using "$reg\FULLsample`is'", keepusing(pre* post* bought_1`is' incl_1`is' never wt_cemm)
	keep if _m==3 	
	drop _m
	gen nywt = wt_cemm*ny_ct
	
	*Prepare variables 
	*elix_quality_hwii contains Elixhauser scores for each episode.
	merge 1:1 idcode indexd using "$sdb\elix_quality_hwii", keepusing(elix)
	drop if _m==2
	drop _m
	
	replace cost17  = cost17 / 0.9762	
	drop if missing(cost17) | missing(tot_drgwt)
	foreach var in age len elix {
	qui sum `var' [aw=nywt], d 
	replace `var' = r(mean) if missing(`var') & nywt > 0 
	}
	
	foreach var in cost17 tot_drgwt age len elix {	
	local ptntcontrol `pc' `plan' 
	
	*Patient mix 
	if "`var'"!="cost17" {
	qui reg `var' bought_1`is'           `fe' if (`treat' | `control') [aw=nywt], vce(cluster num_prv) 
	outreg2 using "$op\regs_FULLp_`is'.xls", append `print' nocons ctitle("`is'_`var'_`mdc'") 
	
	qui reg `var' bought_1`is' tot_drgwt `fe' if (`treat' | `control') [aw=nywt], vce(cluster num_prv) 
	outreg2 using "$op\regs_FULLp_`is'.xls", append `print' nocons ctitle("`is'_DW_`var'_`mdc'") 
	qui tabout pre1_1`is' if `treat' [aw=nywt] using "$op\dvmean_FULLp_`is'.xls", sum cells(mean `var' N `var') total("nowt") f(3) append	
	}
	
	*Patient-level, DRG fixed effects
	if "`var'"=="cost17" {
	qui reghdfe `var' bought_1`is' `hospcontrol' `ptntcontrol' if (`treat' | `control') [aw=nywt], absorb(i.drg i.year i.num) vce(cluster num_prv) keepsing
	outreg2 using "$op\regs_FULLp_`is'.xls", append `print' nocons ctitle("`is'_nowti.DRG_`var'_`mdc'") 
	}
	}
	}